import multiprocessing
import threading
import time


def sub_send(queue):
    while True:
        time.sleep(1)
        # 向队列发送消息
        queue.put("is sub msg")


def sub_rev(queue):
    while True:
        time.sleep(1)
        # 从队列接收消息
        msg = queue.get()
        print(f"Sub Received: {msg}")


def sub_worker(queue):
    threading.Thread(target=sub_send, args=(queue,)).start()
    threading.Thread(target=sub_rev, args=(queue,)).start()


def main_send(queue):
    while True:
        time.sleep(1)
        # 向队列发送消息
        queue.put("is main msg!")


def main_rev(queue):
    while True:
        time.sleep(1)
        # 从队列接收消息
        msg = queue.get()
        print(f"Main Received: {msg}")


if __name__ == '__main__':
    # 创建队列
    queue = multiprocessing.Queue()

    # 创建子进程
    sub_proc = multiprocessing.Process(target=sub_worker, args=(queue,))
    # 启动子进程
    sub_proc.start()
    threading.Thread(target=sub_send, args=(queue,)).start()
    threading.Thread(target=main_rev, args=(queue,)).start()


    # 等待子进程结束
    sub_proc.join()
